package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.config.BusinessException;
import com.emergency.data.entity.SettlementPoint;
import com.emergency.data.entity.SettlementPointSupplies;
import com.emergency.data.vo.SettlementPointDetailsVO;
import com.emergency.data.vo.SettlementPointSuppliesVO;
import com.emergency.data.vo.SettlementPointVO;
import com.emergency.http.DefaultResponseCode;
import com.emergency.mapper.SettlementPointMapper;
import com.emergency.mapper.SettlementPointSuppliesMapper;
import com.emergency.service.SettlementPointService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SettlementPoint)安置点服务实现类
 *
 * @author makejava
 * @since 2020-09-21
 */
@Service
public class SettlementPointServiceImpl extends ServiceImpl<SettlementPointMapper, SettlementPoint> implements SettlementPointService {

    @Resource
    private SettlementPointMapper settlementPointMapper;

    @Resource
    private SettlementPointSuppliesMapper settlementPointSuppliesMapper;

    @Override
    public List<SettlementPointVO> getSettlementPointList(Integer streetId,String level, String area, Integer capacity, String siteName) {
        //查询条件
        QueryWrapper<SettlementPoint> query = new QueryWrapper<>();
        if (streetId != null) {
            query.eq("street_id", streetId);
        }
        if (level != null) {
            query.eq("level", level);
        }
        if (area != null) {
            query.ge("area", area);
        }
        if (capacity != null) {
            query.ge("capacity", capacity);
        }
        if (siteName != null) {
            query.like("site_name", siteName);
        }
        return settlementPointMapper.selectList(query).stream().map(c -> {
            SettlementPointVO vo = new SettlementPointVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public SettlementPointDetailsVO getSettlementPointDetail(Long id) {
        SettlementPoint point = settlementPointMapper.selectById(id);
        if (point == null) {
            throw new BusinessException(DefaultResponseCode.SETTLEMENT_POINT_NOT_EXIST);
        }
        //返回对象VO
        SettlementPointDetailsVO detailsVO = new SettlementPointDetailsVO();
        BeanUtil.copyProperties(point, detailsVO);
        //物资点列表
        List<SettlementPointSuppliesVO> suppliesList = settlementPointSuppliesMapper.selectList(new QueryWrapper<SettlementPointSupplies>()
                .eq("settlement_point_id", id)).stream().map(c -> {
            SettlementPointSuppliesVO vo = new SettlementPointSuppliesVO();
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
        detailsVO.setSuppliesList(suppliesList);
        return detailsVO;
    }
}
